import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This class is add user UI
 */
public class AddUserUI extends JPanel {
	/**
	 * Label for user id
	 */
	private JLabel lblUserID = null;
	/**
	 * Label for user password
	 */
	private JLabel lblUserPass = null;
	/**
	 * Label for user name
	 */
	private JLabel lblUserName = null;
	/**
	 * Label for teacher hours
	 */
	private JLabel lblHours = null;
	/**
	 * title for the current window  
	 */
	private JLabel lbltitle = new JLabel();
	/**
	 * add user button  
	 */
	private JButton btnAddUser = null;
	/**
	 * delete user button  
	 */
	private JButton btnDelUser = null;
	/**
	 * text field for id 
	 */
	private JTextField IdText = null;
	/**
	 * text field for password 
	 */
	private JTextField PassText = null;
	/**
	 * text field for name 
	 */
	private JTextField nameText = null;
	/**
	 * chosen teacher hours option  
	 */
	private JComboBox hoursBox = null;
	/**
	 * back button
	 */
	public JButton btnBack = null;
	/**
	 * help button
	 */
	public JButton btnHelp = null;
	/**
	 * option variable
	 */
	private OptWin opt = null;
	/**
	* Variables to set the input from the gui to the control
	*/
	String id;
	String pass;
	String name;
	int userType;
	int hours=0;
	/**
	* Constructs 
	*/
	public AddUserUI(int num) {
		super();
		userType = num;
		initialize();
	}
	
	

	private void initialize() {
		System.out.println("init AddUserUI");
		btnAddUser = new JButton();
		/**
		 *  add user Implementation
		 */
		btnAddUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				User use = new User(id,pass,name,hours);
				SQL s = new SQL(use,userType);
				s.accept();
			}
		});
		btnAddUser.setBounds(new Rectangle(71, 226, 128, 31));
		btnAddUser.setText("Add New User");
		
		switch (userType)
		{
			case 4:
				lbltitle = new JLabel("Add New Student");
				break;
			case 6:
				lbltitle = new JLabel("Add New Teacher");
				this.add(getHoursBox(), null);
				lblHours = new JLabel("Teacher hourse:");
				lblHours.setBounds(73, 180, 110, 14);
				this.add(lblHours);	
				break;
		}
		this.add(btnAddUser,null);
		
		
		IdText = new JTextField();
		IdText.setBounds(new Rectangle(154, 69, 128, 25));
		IdText.addFocusListener(new java.awt.event.FocusAdapter() 
		{
			public void focusLost(java.awt.event.FocusEvent e) 
			{
				System.out.println("focusLost()"); 
				id=IdText.getText();			
			}
		}
		);
	
		this.add(IdText,null);
		
		nameText = new JTextField();
		nameText.setBounds(new Rectangle(154, 140, 128, 25));
		nameText.addFocusListener(new java.awt.event.FocusAdapter() 
		{
			public void focusLost(java.awt.event.FocusEvent e) 
			{
				System.out.println("focusLost()"); 
				name=nameText.getText();			
			}
		}
		);
	
		this.add(nameText,null);
		
		PassText = new JTextField();
		PassText.setBounds(new Rectangle(154, 105, 128, 25));
		PassText.addFocusListener(new java.awt.event.FocusAdapter() 
		{
			public void focusLost(java.awt.event.FocusEvent e) 
			{
				System.out.println("focusLost()"); 
				pass=PassText.getText();			
			}
		}
		);
		this.add(PassText,null);
		
		this.setVisible(true);
		
		
		

		lblUserID = new JLabel("User ID:");
		lblUserID.setBounds(100, 74, 110, 14);
		this.add(lblUserID);	
		
		
		lblUserPass = new JLabel("Password:");
		lblUserPass.setBounds(83, 110, 110, 14);
		this.add(lblUserPass);
		
		lblUserName = new JLabel("User Name:");
		lblUserName.setBounds(83, 145, 110, 14);
		this.add(lblUserName);	

	
		lbltitle.setBounds(172, 26, 110, 14);
		this.add(lbltitle);
		
		this.setSize(400, 321);
		this.setLayout(null);

	
		btnBack = new JButton("Back");
		btnBack.setBounds(169, 280, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Enter user details and then click on","'Add New User' or 'Delete User' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		btnDelUser = new JButton();
		btnDelUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				User use = new User(id,pass);
				SQL s = new SQL(use,userType+6);
				s.accept();
			}
		});
		btnDelUser.setText("Delete User");
		btnDelUser.setBounds(new Rectangle(68, 210, 110, 31));
		btnDelUser.setBounds(235, 226, 121, 31);
		this.add(btnDelUser);
		this.setVisible(true);
		
	}
	
	private JComboBox getHoursBox() {
		if (hoursBox == null) {
			hoursBox = new JComboBox();
			hoursBox.setBounds(new Rectangle(180, 180, 45, 25));  
				for (Integer i=0; i<31 ; i++)
				{
					hoursBox.addItem(i.toString());
				}
				
				hoursBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					hours = hoursBox.getSelectedIndex();
					System.out.println("day is : " + hours); 
				}
			});
		}
		return hoursBox;
	}
	

}
